import './../sass/header.scss';
import './../sass/questionbank.scss';
import './../font/iconfont.css';
import './../js/auth.js'

const $ = require('jquery')

$(".testtitle").text(`${decodeURI(location.search.substring(1).replace('title=', ''))}题库`);

// 题库渲染
$.ajax({
    url: '/auth/token',
    type: 'post',
    success({ message, code, user }) {
        $.ajax({
            url: '/tests/getTests',
            type: 'post',
            success({ code, message, result }) {
                if (code == 1) {
                    if (decodeURI(location.search.substring(1).replace('title=', '')) == "顺序练习") {
                        render(result, user);
                    } else {
                        const typeTests = result.filter(item => item.typeId.type == decodeURI(location.search.substring(1).replace('title=', '')));
                        render(typeTests, user);
                    }
                }
            }
        })
    }
})

// 已考&未考
$("#testStatus").on('change', function () {
    const tests = $("#tests section");
    switch ($(this).val()) {
        case 'total':
            $("#tests section").show();
            break;
        case 'tested':
            $("#tests section").hide();
            [...tests].forEach(item => {
                if ($(item).find("button").attr("class") == "istested") {
                    $(item).show();
                }
            });
            break;
        case 'untest':
            $("#tests section").hide();
            [...tests].forEach(item => {
                if ($(item).find("button").attr("class") != "istested") {
                    $(item).show();
                }
            });
            break;

        default:
            break;
    }
})


function render(typeTests, user) {
    const tests = typeTests.map(item =>
        `<section>
                <p class="title">${item.title}</p>
                <p>考试类型: <span>${item.typeId.type}</span></p>
                <p>考试地址: <span>信息中心会议室</span></p>
                <p>已考人数: <span>343人</span></p>
                <button data-testId="${item._id}">参加考试</button>
            </section>`
    )
    $("#tests").html(tests);
    const testItems = $("#tests section");
    [...testItems].forEach(test => {
        $.ajax({
            url: '/testeds/getTested',
            data: {
                studentId: user.user._id,
            },
            type: 'post',
            success({ message, code, result }) {
                const testeds = result.filter(i => i.studentId._id == user.user._id);
                const istested = testeds.some(tested => tested.testId._id == $(test).find("button").attr("data-testid"))
                if (istested) {
                    $(test).find('button').addClass("istested").text("已经考试,查看解析");
                }
            }
        })
    });
}

$("#tests").on('click', 'section button', function () {
    console.log($(this).attr("class"));
    if ($(this).attr("class") == 'istested') {
        location.assign(`./../html/testpaper.html?id=${$(this).attr('data-testId')}&status=tested`)
    } else {
        location.assign(`./../html/testinfo.html?id=${$(this).attr('data-testId')}`)
    }
})